/**
 * Stub connection to test server without network.
 */

package egs.comms;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.util.Collections;
import java.util.List;

import egs.packets.InvalidPacketException;
import egs.packets.Packet;


public class StubConnection extends Connection {
	
	private List<Packet> pkts;
	private String name;
	
    public StubConnection(List<Packet> pkts, String name) {
    	super(0);
    	this.pkts = Collections.synchronizedList(pkts);
    	this.name = name;
   	}
    
    public void addPackets(List<Packet> pkts) {
    	this.pkts.addAll(pkts);
    }
    
    public void addPacket(Packet pkt) {
    	this.pkts.add(pkt);
    }
    
    public void setStreams() { }
    public void setStreams(DataOutputStream outs, DataInputStream ins) { }
    public void sendPacket(Packet p) throws EGSCommunicationException {
    	System.out.println("\nServer sending to " + name + ":\n" + p);
    }

    public Packet getPacket() throws InvalidPacketException {
    	
    	// block until we have a packet
    	while (pkts.size() < 1)
			try { Thread.sleep(10); } catch (InterruptedException e) { }
			
    	if (pkts.size() > 0) {
    		Packet pkt = pkts.remove(0);
    		System.out.println("\n" + name + " sent to server:\n" + pkt);
    		return pkt;
    	} else {
    		return null;
    	}
	}

    public void closeConnection() { }
    public void sendBytes(byte[] data) { }
}
